﻿using System;
using System.Html;
using Quicklight.Web.Html;
using jQueryApi;

namespace Quicklight.Web.Razor {

   /// <summary>
   /// Does the binding on behalf of a declarative binding of a Validation to an attribute on an element
   /// For example <input type="text" @Binding("value", ViewModel.X.Validation) />
   /// </summary>
   public class ValidationAttributeBinder : AttributeBinder {
      private Validation validation;

      /// <summary>
      /// Constructs a ValidationAttributeBinder for binding the Validation to the attribute 
      /// </summary>
      /// <param name="attributeName">Name of attribute to bind to</param>
      /// <param name="validation">Validation to bind to attribute</param>
      public ValidationAttributeBinder(string attributeName, Validation validation)
         : base(attributeName) {

            this.validation = validation;
      }

      /// <summary>
      /// Bind method does the actual binding of the Validation to the attribute of the specified element
      /// </summary>
      /// <param name="element">Element with which the binding must occur</param>
      protected override void Bind(Element element) {
         element.Bind(attributeName, validation);

         jQueryObject jQueryObject = jQuery.FromElement(element);
         jQueryObject.Attribute(attributeName, validation.Message);
      }
   }
}
